package com.tj.controller;

import com.tj.pojo.SetMeal;
import com.tj.service.SetMealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/setMeal")
public class SetMealController {

    @Autowired
    SetMealService setMealService;

    // ================================= 查询用户 =================================
    /*
     * -取得用户列表数据
     */
    @RequestMapping("/getSetMealList")
    public Map<String, Object> getSetMealList() throws Exception {
        List<SetMeal> setMealList = setMealService.findAllList();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("status", "1");
        map.put("setMealList",setMealList);
        return map;
    }

    /*
     * -按姓名进行模糊查找
     */
    @RequestMapping("/getSetMealListByName")
    public Map<String, Object> getSetMealListByName(@RequestParam String name) throws Exception {
        List<SetMeal> setMealList = setMealService.findSetMealListByName(name);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("status", "1");
        map.put("setMealList", setMealList);
        return map;
    }

    /*
     * -按ID查
     */
    @RequestMapping("/getSetMealById")
    public Map<String, Object> getSetMealById(@RequestParam int smId) throws Exception {
        SetMeal setMeal = setMealService.findSetMealById(smId);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("status", "1");
        map.put("setMeal", setMeal);
        return map;
    }

    // ================================= 增加用户 =================================
    @RequestMapping("/insertSetMeal")
    public int insertSetMeal(@RequestBody SetMeal setMeal) {
        return setMealService.insertSetMeal(setMeal);
    }
    // ================================= 修改用户 =================================
    @RequestMapping("/updateSetMeal")
    public int updateSetMeal(@RequestBody SetMeal setMeal) {
        return setMealService.updateSetMeal(setMeal);
    }
    // ================================= 删除用户 =================================
    /*
     * -执行用户删除的操作
     */
    @RequestMapping("/setDelete")
    public Map<String, Object> setDelete(HttpSession session, SetMeal setMeal) throws Exception {
        int num = setMealService.deleteSetMeal(setMeal);
        Map<String, Object> map = new HashMap<String, Object>();
        if (num > 0) {
            map.put("status", "1");
        } else {
            map.put("status", "0");
        }
        return map;
    }

}
